﻿using Business.Uin;
using Models.WebChat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeChat.Handlers;

namespace WeChat.Controllers
{
    public class WebChatController : Controller
    {
        private UinInfoManager _UinManager = new UinInfoManager();
        private MessageInfoManager _MessageInfoManager = new MessageInfoManager();
        private UinGroupInfoManager _UinGroupManager = new UinGroupInfoManager();
        private UinSocketManager _UinSocketManager = new UinSocketManager();

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login(string name, string password)
        {

            var uinid = _UinManager.Login(name, password);
            if (uinid != -1)
            {
                HttpContext.Session["uinid"] = uinid;
                return Content(uinid.ToString());
            }
            else
            {
                return Content("登录失败，账号或密码有误！");
            }
        }
        [HttpGet]
        public ActionResult Chat(string uinid)
        {
            if (string.IsNullOrEmpty(uinid))
                return View("Index");
            var tempUinId = HttpContext.Session["uinid"];
            ViewBag.UinId = tempUinId;
            if ((tempUinId != null && tempUinId.ToString().Equals(uinid)))
                return View();
            else
                return View("Index");
        }

        public ActionResult GetAllUser()
        {
            return Json(new { data = _UinManager.GetAllUserInfo() });
        }
        public ActionResult GetAllGroup()
        {
            var userGroups = _UinGroupManager.GetUingroupidList((int)HttpContext.Session["uinid"]);
            var data = _UinManager.GetAllGroupInfo().Where(g => userGroups.Contains(g.Id)).ToList();
            return Json(new { data = data });
        }

        public ActionResult GetMembers(int uinid)
        {
            var uinInfo = _UinManager.GetUinInfoById(uinid);
            var uinInfList = new List<UinInfo>();
            if (uinInfo != null)
            {
                if (uinInfo.Uin_type == Models.WebChat.UinType.GROUP)
                {
                    var userids = _UinGroupManager.GetUinuseridList(uinInfo.Id);
                    var uinList = _UinManager.GetAllUserInfo().Where(u => userids.Contains(u.Id)).ToList();
                    uinInfList.AddRange(uinList);
                }
                else
                {
                    uinInfList.Add(uinInfo);
                }
            }
            return Json(new { data = uinInfList });
        }

        public void ChatSocket()
        {
            if (HttpContext.IsWebSocketRequest)
            {
                //var uinId = HttpContext.Request["uinid"];
                //HttpContext.AcceptWebSocketRequest(WebSocketRequestHandler);
                HttpContext.AcceptWebSocketRequest(SocketContextHandler.WebSocketRequestHandler);
            }
        }
    }
}